<template>
  <section class="loginContainer">
    <div class="zhuche">
      <div class="zhuche1">登陆</div>
    </div>
    <img class="tupian"
         src="../img/logo.png"
         alt="" />
    <div class="loginInner">
      <div class="login_header"></div>
      <div class="login_content">
        <form @submit.prevent="login">
          <!-- 密码登陆 -->
          <div :class="{ on: loginWay }">
            <section>
              <section class="login_message">
                <input type="text"
                       maxlength="11"
                       placeholder="账号"
                       v-model="username" />
              </section>
              <section class="login_verification">
                <!-- 是否显示密码 -->
                <input type="text"
                       maxlength="8"
                       placeholder="密码"
                       v-if="showPwd"
                       v-model="password" />
                <input type="password"
                       maxlength="8"
                       placeholder="密码"
                       v-else
                       v-model="password" />

                <div class="switch_button off"
                     :class="showPwd ? 'on' : 'off'"
                     @click="showPwd = !showPwd">
                  <div class="switch_circle"
                       :class="{ right: showPwd }"></div>
                  <span class="switch_text">{{ showPwd ? "" : "" }}</span>
                </div>
              </section>
            </section>
          </div>
          <div class="forgetPassWord">
            <div @click="$router.push('/register')">注册账号</div>
            <div @click="forgetPassWord">忘记密码</div>
          </div>
          <button class="login_submit"
                  @click.prevent="login">登陆</button>
        </form>
        <div class="qq">
          <i class="weibo iconfont icon-QQ"
             style="color: #3ecbf0"></i>
          <i class="weibo iconfont icon-weixin"
             style="color: #1bc96e"></i>
          <i class="weibo iconfont icon-shejiaotubiao-06"
             style="color: #ec5e5e"></i>
        </div>
      </div>
    </div>
    <van-dialog v-model="showForget"
                title="找回密码"
                width="350px"
                theme="round-button"
                @confirm="querenForget"
                confirm-button-color="skyblue"
                :show-confirm-button="showConfirm"
                show-cancel-button>
      <div style="margin-top:10px">
        <van-cell-group>
          <van-field v-model="forgetName"
                     label="用户名:"
                     required
                     @blur="pandunUserName"
                     left-icon="manager-o"
                     placeholder="请输入用户名" />
        </van-cell-group>
        <van-cell-group>
          <van-field v-model="phone"
                     center
                     clearable
                     left-icon="phone-o"
                     label="手机号"
                     required
                     @blur="panduanPhone"
                     placeholder="请输入手机号">
            <template #button>
              <van-button size="mini"
                          @click="sendSms"
                          :disabled="buttonSend"
                          type="primary">{{sendMessage}}</van-button>
            </template>
          </van-field>
        </van-cell-group>
        <van-cell-group>
          <van-field v-model="code"
                     center
                     clearable
                     required
                     @blur="panduanCode"
                     left-icon="other-pay"
                     label="短信验证码"
                     placeholder="请输入验证码">
          </van-field>
        </van-cell-group>

        <van-cell-group>
          <van-field v-model="forgetPassword"
                     label="新密码:"
                     required
                     left-icon="records"
                     type="password"
                     @blur="newPassword"
                     placeholder="请输入新密码" />
        </van-cell-group>
      </div>
    </van-dialog>
    <!-- 提示组件,closeTip事件在其中被分发出来 -->
    <!-- <AlertTip :alertText="alertText" v-show="alertShow" @closeTip="closeTip" /> -->
    <alerTip :alertText="alertText"
             v-show="alertShow"
             @closeTip="closeTip"></alerTip>
  </section>
</template>
<script>
import AlerTip from "../../components/AlertTip/AlertTip.vue";
import { loginApi, forgetPassword, sendDuanxin } from "../../api/getData.js";
export default {
  components: {
    AlerTip
  },
  data () {
    return {
      jizhuwoData: false,
      loginWay: true, // true为短信登陆，false为密码登陆     
      showPwd: false, // 是否显示密码
      username: "", // 用户名
      password: "",// 密码
      rememberme: false,//状态
      alertText: '',//提示文本
      alertShow: false,//是否显示警告框
      showForget: false,
      forgetName: "",
      forgetPassword: "",
      code: "",
      phone: "",
      buttonSend: false,
      sendMessage: "发送验证码",
      showConfirm: true,
      responseCode: ""
    };
  },
  methods: {
    // 记住我
    jizhuwo () {
      this.jizhuwoData = true
    },
    // 显示提示框
    showAlert (alertText) {
      this.alertShow = true;
      this.alertText = alertText;
    },
    // 异步登陆
    async login () {
      // 登陆请求返回结果
      let result;
      // 前台表单验证
      if (this.loginWay) {
        // 密码登陆
        const { username, password, rememberme } = this;
        if (!this.username) {
          // 用户名不能空
          this.showAlert("用户名不能空");
          return;
        } else if (!this.password) {
          // 密码不能空
          this.showAlert("密码不能空")
          return;
        }
        // 发送ajax请求密码登陆
        result = await loginApi(username, password, rememberme);
        // console.log(result);
        if (result.data.status === 200) {
          this.$message.success('登陆成功');
          // window.sessionStorage.setItem('userData', JSON.stringify(result.data.data))
          // window.sessionStorage.setItem('token', JSON.stringify(result.data.data.ticket))
          window.localStorage.setItem('token', JSON.stringify(result.data.data.ticket))
          window.localStorage.setItem('userData', JSON.stringify(result.data.data))
          this.$router.push('/')
        } else if (result.data.status !== 200) {
          this.$message.fail(result.data.msg);
        }
      }

      // 停止计时
      if (this.codeTime) {
        clearInterval(this.intervalId);
        this.codeTime = 0;
      }
    },
    // 关闭提示框
    closeTip () {
      this.alertShow = false;
      this.alertText = "";
    },
    // 忘记密码
    forgetPassWord () {
      this.forgetName = ""
      this.phone = ""
      this.forgetPassword = ""
      this.code = ""
      this.showForget = true
    },
    sendSms () {
      forgetPassword(this.forgetName, this.phone).then(res => {
        if (res.data.msg !== "密码不能为空") {
          this.$message("手机号不存在")
          this.buttonSend = true
          this.showConfirm = false
        } else {
          this.$message("发送成功")
          sendDuanxin(this.phone).then(res => {
            this.responseCode = res.data.data
          })
          let miao = 30
          this.buttonSend = true
          this.sendMessage = miao
          var timer = setInterval(() => {
            miao--
            this.sendMessage = miao
            if (miao === 0) {
              this.sendMessage = "发送验证码"
              this.buttonSend = false
              clearInterval(timer)
            }
          }, 1000)
        }
      })
    },
    pandunUserName () {
      if (this.forgetName.trim() === "") {
        this.$message("用户名不能为空")
        this.showConfirm = false
      } else {
        forgetPassword(this.forgetName).then(res => {
          if (res.data.msg !== "手机号不存在") {
            this.$message("用户不存在")
            this.showConfirm = false
          }
        })
      }
    },
    panduanCode () {
      if (this.code.trim() === "") {
        this.$message("验证码不能为空")
        this.showConfirm = false
      } else {
        this.showConfirm = true
        if (this.responseCode !== this.code) {
          this.$message("验证码错误")
          this.showConfirm = false
        }
      }
    },
    panduanPhone () {
      if (/^1([38][0-9]|4[579]|5[0-3,5-9]|6[6]|7[0135678]|9[89])\d{8}$/
        .test(this.phone)) {
        this.buttonSend = false
        this.showConfirm = true
        forgetPassword(this.forgetName, this.phone).then(res => {
          if (res.data.msg !== "密码不能为空") {
            this.$message("绑定手机号不正确")
            this.buttonSend = true
            this.showConfirm = false
          }
        })
      } else {
        this.$message("手机号格式错误")
        this.buttonSend = true
        this.showConfirm = false
      }
    },
    // 点击确认按钮
    querenForget () {
      if (this.forgetPassword.trim() === "" || this.code.trim() === "" || this.phone.trim() === "" || this.forgetName.trim() === "") {
        this.$message("参数有误，请重新输入")
        setTimeout(() => {
          this.showForget = true
        }, 600)
      } else {
        forgetPassword(this.forgetName, this.phone, this.forgetPassword).then(res => {
          this.$message(res.data.data)
        })
      }
    },
    newPassword () {
      if (this.forgetPassword.trim() === "") {
        this.$message("密码不能为空")
        this.showConfirm = false
      } else {
        this.showConfirm = true
      }
    }
  }

};
</script>
<style lang="scss" scoped>
.loginContainer {
  height: 100%;
  width: 100%;
  position: absolute;
  top: 0px;
  bottom: 0px;

  .zhuche {
    width: 100%;
    height: 40px;
    background-color: #dcd9d4;
    line-height: 40px;
    position: relative;
  }

  .zhuche1 {
    text-align: center;
    font-weight: bold;
    font-size: 20px;
    text-shadow: 1px 1px 5px #ccc;
    background-image: linear-gradient(to right, #c0ebd8 0%, #c1e6f0 100%);
  }

  .tupian {
    border-radius: 50%;
    width: 250px;
    height: 250px;
    position: relative;
    left: 50%;
    transform: translate(-50%, 0);
  }

  .loginInner {
    width: 80%;
    margin: 0 auto;

    .login_header {
      .login_logo {
        font-size: 40px;
        font-weight: bold;
        color: limegreen;
        text-align: center;
      }

      .login_header_title {
        padding-top: 40px;
        text-align: center;

        > a {
          color: #333;
          font-size: 14px;
          padding-bottom: 4px;

          &:first-child {
            margin-right: 40px;
          }

          &.on {
            color: limegreen;
            font-weight: 700;
            border-bottom: 2px solid limegreen;
          }
        }
      }
    }

    .login_content {
      > form {
        > div {
          display: none;

          &.on {
            display: block;
          }

          input {
            width: 100%;
            height: 100%;
            padding-left: 10px;
            box-sizing: border-box;
            border: 1px solid #ddd;
            border-radius: 4px;
            outline: 0;
            font: 400 14px Arial;
            border-radius: 30px;
            position: absolute;

            &:focus {
              border: 1px solid limegreen;
            }
          }

          .login_message {
            position: relative;
            margin-top: 16px;
            height: 48px;
            font-size: 14px;
            line-height: 48px;

            .get_verification {
              position: absolute;
              top: 50%;
              right: 10px;
              transform: translateY(-50%);
              border: 0;
              color: #ccc;
              font-size: 14px;
              background: transparent;

              &.right_phone {
                color: black;
              }
            }
          }

          .login_verification {
            position: relative;
            margin-top: 16px;
            height: 48px;
            font-size: 14px;

            .switch_button {
              font-size: 12px;
              border: 1px solid #ddd;
              border-radius: 8px;
              transition: background-color 0.3s, border-color 0.3s;
              padding: 0 6px;
              width: 30px;
              height: 16px;
              line-height: 16px;
              color: #fff;
              position: absolute;
              top: 50%;
              right: 10px;
              transform: translateY(-50%);

              &.off {
                background: #fff;

                .switch_text {
                  float: right;
                  color: #ddd;
                }
              }

              &.on {
                background: limegreen;
              }

              > .switch_circle {
                // transform translateX(27px)
                position: absolute;
                top: -1px;
                left: -1px;
                width: 16px;
                height: 16px;
                border: 1px solid #ddd;
                border-radius: 50%;
                background: #fff;
                box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.1);
                transition: transform 0.3s;

                &.right {
                  transform: translateX(30px);
                }
              }
            }
          }

          .login_hint {
            margin-top: 12px;
            color: #999;
            font-size: 14px;
            line-height: 20px;

            > a {
              color: limegreen;
            }
          }
        }

        .login_submit {
          display: block;
          width: 100%;
          height: 42px;
          margin-top: 10px;
          border-radius: 30px;
          background-image: linear-gradient(to right, #c0ebd8 0%, #c1e6f0 100%);
          color: #fff;
          text-align: center;
          font-size: 16px;
          line-height: 42px;
          border: 1px solid white;
          font-weight: bold;
          outline: none;
        }
      }

      .about_us {
        display: block;
        font-size: 12px;
        margin-top: 20px;
        text-align: center;
        color: #999;
      }
    }

    .go_back {
      position: absolute;
      top: 5px;
      left: 5px;
      width: 30px;
      height: 30px;

      > .iconfont {
        font-size: 20px;
        color: #999;
      }
    }

    .qq {
      width: 100%;
      height: 50px;
      margin-top: 40px;
      display: flex;
      justify-content: space-around;
      line-height: 50px;
    }

    .weibo {
      display: block;
      font-size: 35px;
    }
  }
}
.forgetPassWord {
  height: 45px;
  display: flex !important;
  justify-content: space-between;
  line-height: 45px;
}
.forgetPassWord div {
  font-size: 14px;
  color: rgb(100, 100, 100);
  font-weight: 800;
}
</style>
